<template>
  <div class="container">
    <el-card class="login-box">
      <div>
        <h3>用户登录</h3>
      </div>
      <el-form>
        <el-form-item>
          <el-input prefix-icon="el-icon-user-solid" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input show-password  prefix-icon="el-icon-s-goods" v-model="user.password"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" class="loginbtn" @click="login">登录</el-button>
        </el-form-item>
      </el-form>
      <div>
        您还没有账号，请<router-link to="/register">注册</router-link>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name:'Login',
  data(){
   return{
     user:{
      username:'',
      password:''
    }
   }
  },
  methods:{
    async login(){
      const result=await this.$api.users.loginApi(this.user)
      if(result.code){
        this.$message.success(result.message)
        //保存token到本地
        localStorage.setItem('token',result.token)
        //跳转
        this.$router.replace('/home')
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.container {
  width: 100vw;
  min-height: 100vh;
  background-image: url('../assets/bg.jpg');
  background-size: 100%;
}
.login-box {
  position:absolute;
  width: 500px;
  right: 100px;
  top: 200px;
}
.loginbtn{
    width: 460px;
}
</style>